package com.example.scheduled.service;

import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Author: Jiangxx
 * @Date: 2022/04/13
 * @Description:
 */
@Service
public class ScheduleTest {

    @Async("getAsyncExecutor")
    @Scheduled(fixedDelay = 20 * 1000, initialDelay = 10 * 1000)
    public void fixedDelayScheduled() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println("********************fixedDelay配置Scheduled执行********************"+ format.format(new Date()));
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }


    @Async("getAsyncExecutor")
    @Scheduled(fixedRate = 10 * 1000)
    public void fixedRateScheduled() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println("********************fixedRate配置Scheduled执行********************"+ format.format(new Date()));
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Async("getAsyncExecutor")
    @Scheduled(cron = "10 * * * * ?")
    public void cronScheduled() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println("********************fixedRate配置Scheduled执行********************"+ format.format(new Date()));
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}
